package cn.schoolwow.workflow.test.module.oa.loan.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.dto.FlowContextDataRange;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.check.instance.CheckInstance;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.test.module.analyzeWorkReport.audit.single.common.PrintWorkFlowInstanceHistoryFlow;

public class LoanAuditFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        startWorkFlow(flowContext);
        audit(flowContext);
        printLog(flowContext);
        checkStatus(flowContext);
    }

    @Override
    public String name() {
        return "人工审批贷款工作流";
    }

    private void startWorkFlow(FlowContext flowContext){
        String deployName = flowContext.checkData("deployName", String.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        StartWorkFlowRequest startWorkFlowRequest = new StartWorkFlowRequest();
        startWorkFlowRequest.definitionName = deployName;
        startWorkFlowRequest.initiator = "张三";
        startWorkFlowRequest.contextData.put("field102", 30);
        startWorkFlowRequest.contextData.put("field103", 600);
        flowContext.putInstanceData(startWorkFlowRequest, FlowContextDataRange.ContextThreadLocal);

        WorkFlowInstance workFlowInstance = quickWorkFlow.startWorkFlow(startWorkFlowRequest);
        flowContext.putCurrentFlowInstanceData(workFlowInstance);
        flowContext.putCurrentFlowData("workFlowInstanceId", workFlowInstance.getId());
    }

    private void audit(FlowContext flowContext){
        Long workFlowInstanceId = flowContext.checkData("workFlowInstanceId", Long.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.instanceIdList.add(workFlowInstanceId);
        workFlowTaskQuery.assignerList.add("0:1");
        WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
        quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
    }

    private void printLog(FlowContext flowContext){
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        flowContext.startFlow(new PrintWorkFlowInstanceHistoryFlow())
                .putCurrentCompositeFlowInstanceData(workFlowInstance)
                .execute();
    }

    private void checkStatus(FlowContext flowContext){
        Long workFlowInstanceId = flowContext.checkData("workFlowInstanceId", Long.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        WorkFlowInstance workFlowInstance = quickWorkFlow.getSingleWorkFlowInstance(workFlowInstanceId);
        CheckInstance.newCheck().fieldValueCheck(workFlowInstance.getStatus(), WorkFlowInstanceStatus.Finished.status, "实例状态");
    }
}
